//加载公共样式
require('../../assets/css/index.less')

//加载自己的样式
require('./login.less')

// 引入axios
const axios = require('axios')

// 引入toast
const t = require('../../utils/toast')

// 引入window
const { $, g } = require('../../utils/dom')
$(function () {

    g('.submit').addEventListener('click', () => {
        let errMsg = '';
        // 判断验证
        if (!/^1[3-9]\d{9}$/.test(g('#phoneIpt').value)) {
            errMsg = '请输入正确手机号'
        } else if (!/^\w{3,6}$/.test(g('#pwdIpt').value)) {
            errMsg = '请输入密码'
        } else {
            axios.post('http://fitness.h5.itsource.cn:3701/api/user/login', {
                account: g('#phoneIpt').value,
                password: g('#pwdIpt').value
            }).then((res) => {
                if (res.data.errno === 0) {
                    // console.log('res :>> ', res);
                    localStorage.setItem('token', res.data.data.token);
                    t.toast('登录成功');
                    setTimeout(() => {
                        location.href = './home.html';
                    }, 2500)
                } else {
                    t.toast('登录失败');
                }
            })
        }

        if (errMsg) {
            msgTip.textContent = errMsg
            // console.log('成功登录 :>> ', "成功登录");
        }
    })
})
